﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Markup;
using DeepEarth.BingMapsToolkit.Client.MapGeometry;
using Microsoft.Maps.MapControl;
using Microsoft.Maps.MapControl.Core;

using Map = Microsoft.Maps.MapControl.Map;

namespace SMIL.Client.WPXAP.PushPin
{
    public class XAMLPushPin : UserControl, IDisposable
    {
        FrameworkElement LayoutRoot;        
        double currentZoomLevel;
        protected MapCore MapInstance;        

        protected ScaleTransform scaleTransform;

        public EventHandler LoadAnimationCompleted { get; set; }
        public EventHandler UnloadAnimationCompleted { get; set; }

        public XAMLPushPin(MapCore map)
        {            
            MapInstance = map;

            Loaded += new RoutedEventHandler(XAMLPushPin_Loaded);
            MapInstance.ViewChangeOnFrame += new EventHandler<MapEventArgs>(MapInstance_ViewChangeOnFrame);        
    
            // "Tomma" eventhandlers
            LoadAnimationCompleted = new EventHandler((o, e) => o = o);
            UnloadAnimationCompleted = new EventHandler((o, e) => o = o);
        }

        public Location PinLocation { get; set; }

        #region IDisposable Members

        public void Dispose()
        {
        }

        #endregion

        public virtual void LoadAnimation()
        {
            LoadAnimationCompleted(null, null);
        }

        public virtual void UnloadAnimation()
        {
            UnloadAnimationCompleted(null, null);
        }
        
        void XAMLPushPin_Loaded(object sender, RoutedEventArgs e)
        {            
            OnApplyTemplate();
            applyScaling();
        }

        void MapInstance_ViewChangeOnFrame(object sender, MapEventArgs e)
        {
            applyScaling();
        }

        public object FindControlChild(string name)
        {
            return LayoutRoot.FindName(name);
        }

        public void LoadFromString(string xaml)
        {
            LayoutRoot = XamlReader.Load(xaml) as FrameworkElement;
            this.Content = LayoutRoot;
            scaleTransform = (ScaleTransform)FindControlChild("_ScaleTransform");
        }

        public void LoadFromFile(string xaml_file)
        {
            System.IO.Stream s = this.GetType().Assembly.GetManifestResourceStream(xaml_file);
            System.IO.StreamReader reader = new System.IO.StreamReader(s);

            LoadFromString(reader.ReadToEnd());
        }

        protected virtual void applyScaling()
        {
            currentZoomLevel = MapInstance.ZoomLevel;

            if (scaleTransform != null)
            {
                double scale = Math.Pow(0.05 * (currentZoomLevel + 5), 2) + 0.01;
                if (scale > 1) scale = 1;
                if (scale < 0.125) scale = 0.125;
                scaleTransform.ScaleX = scale;
                scaleTransform.ScaleY = scale;
            }
        }
    }
}
